package erfgame.core.world.terrain;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


import erfgame.core.Pair;
import erfgame.core.world.World;

public class MappedTerrainColorSourceIndexFactory implements
		TerrainColorSourceIndexFactory {
	private Map<MaterialType, TerrainColorSourceFactory> colorSourceFactories;
	private TerrainColorSourceFactory defaultTerrainColorSourceFactory;
	
	public MappedTerrainColorSourceIndexFactory(			
			Map<MaterialType, TerrainColorSourceFactory> colorSourceFactories,
			TerrainColorSourceFactory defaultTerrainColorSourceFactory
	) {
		this.colorSourceFactories = colorSourceFactories;
		this.defaultTerrainColorSourceFactory = defaultTerrainColorSourceFactory;
	}

	public TerrainColorSourceIndex createIndex(World world) {
		TerrainColorSource defaultColorSource;
		defaultColorSource = this.defaultTerrainColorSourceFactory.createColorSource(world);

		List<Pair<MaterialType, TerrainColorSource>> terrainColorSources;
		terrainColorSources = new ArrayList<Pair<MaterialType, TerrainColorSource>>( this.colorSourceFactories.size() );
		for( Map.Entry<MaterialType, TerrainColorSourceFactory> entry : colorSourceFactories.entrySet() ) {
			TerrainColorSourceFactory colorSourceFactory;
			colorSourceFactory = entry.getValue();
			TerrainColorSource colorSource;
			//colorSource = colorSourceFactory.createColorSource(world, rotation);
			colorSource = colorSourceFactory.createColorSource(world);
			MaterialType materialType = entry.getKey();
			Pair<MaterialType, TerrainColorSource> pair;
			pair = new Pair<MaterialType, TerrainColorSource>( materialType, colorSource );
			terrainColorSources.add(pair);
		}
		
		return new TerrainColorSourceIndex( terrainColorSources, defaultColorSource );
	}

}
